package com.dbb.trade.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.dbb.trade.entity.FilePath;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.ContentHandler;
/**
 * 文件工具，用于对方便机器文件进行批量处理
 * 以增加工具包括，
 * <li>文件复制</li>
 * <li>文件类型获取</li>
 * <li>获取所有文件列表</li>
 * 
 * @author Administrator
 *
 */
public final class FileUtils {
	
	public static final Integer COPY_CAHCE = 2097152;
	
	private static Logger logger = LoggerFactory.getLogger(FileUtils.class);
	
	private FileUtils() {}
	/**
	 * 文件复制
	 * @param from 源文件地址
	 * @param to 目标地址
	 * @return
	 * @throws Exception
	 */
	public static long copy(String from, String to) {
		try {
			return copy(new FileInputStream(from), to);
		} catch (Exception e) {
			logger.error("文件复制失败::" + e.getMessage());
		}
		return 0;
	}
	
	public static void mikeParents(String fileName) {
		File file = new File(fileName);
		if(!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
	}
	
	public static void mikeParents(File file) {
		if(!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
	}
	
	public static long copy(InputStream in, String f2) {
		long time = new Date().getTime();
		FileOutputStream out;
		mikeParents(f2);
		try {
			out = new FileOutputStream(f2);
			byte[] buffer = new byte[COPY_CAHCE];
			while (true) {
				int ins = in.read(buffer);
				if (ins == -1) {
					in.close();
					out.flush();
					out.close();
					return new Date().getTime() - time;
				} else {
					out.write(buffer, 0, ins);
				}
			}
		} catch (Exception e) {
			logger.error("文件复制失败::" + e.getMessage());
			return new Date().getTime() - time;
		}
	}
	/**
	 * 获取文件类型
	 * @param fileName 文件名
	 * @return 文件类型名称
	 */
	public static String getFileContent(String fileName) {
		return getFileContent(new File(fileName));
	}
	
	public static String getFileContent(File file) {
		FileInputStream is = null;
		String fileContent = "";
		try {
			is = new FileInputStream(file);
			ContentHandler contenthandler = new BodyContentHandler();
			Metadata metadata = new Metadata();
			metadata.set(Metadata.RESOURCE_NAME_KEY, file.getName());
			Parser parser = new AutoDetectParser();
			parser.parse(is, contenthandler, metadata, null);
			fileContent = metadata.get(Metadata.CONTENT_TYPE);
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					logger.error(e.getMessage());
				}
			}
		}
		return fileContent;
	}
	/**
	 * 遍历所有文件
	 * @param fileName 文件名
	 * @return 文件列表
	 */
	public static List<File> getAllFiles(String fileName) {
		return getAllFiles(new File(fileName));
	}
    
    public static FilePath getFilePath(String filePath) {
        FilePath fp = new FilePath(filePath);
        String[] fileNames = filePath.split("\\\\");        
//        Collections.addAll(filePaths, fileNames);

        return fp;
    }
	
	public static List<File> getAllFiles(File file) {
		List<File> files = new ArrayList<File>();
    	if(file.isDirectory()) {
    		for(File f : file.listFiles()) {
    			if(f.isDirectory()) {
    				files.addAll(getAllFiles(f));
    			} else {
    				try {
    					files.add(f);
					} catch (Exception e) {
						logger.warn(f.getName() + "::" + e.getMessage());
					}
    			}
    		}
    	}
    	return files;
	}
	
	/**
	 * 获取文件最后一次修改时间
	 * @param file
	 * @return
	 */
	public static Date getLastModifyDate(File file) {
		return new Date(file.lastModified());
	}
	
}
